package com.atguigu.tingshu.album.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.tingshu.album.mapper.*;
import com.atguigu.tingshu.album.service.BaseCategoryService;
import com.atguigu.tingshu.model.album.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@SuppressWarnings({"all"})
public class BaseCategoryServiceImpl extends ServiceImpl<BaseCategory1Mapper, BaseCategory1> implements BaseCategoryService {

    @Autowired
    private BaseCategory1Mapper baseCategory1Mapper;

    @Autowired
    private BaseCategory2Mapper baseCategory2Mapper;

    @Autowired
    private BaseCategory3Mapper baseCategory3Mapper;


    @Autowired
    private BaseCategoryViewMapper baseCategoryViewMapper;

    @Autowired
    private BaseAttributeMapper baseAttributeMapper;

    @Autowired
    private BaseAttributeValueMapper baseAttributeValueMapper;

    @Override
    public List<JSONObject> getBaseCategoryList() {

        List<JSONObject> jsonObjects = new ArrayList<>();

        //查询所有分类
        List<BaseCategoryView> baseCategoryViews = baseCategoryViewMapper.selectList(null);

        //分组获得一级
        //通过Category1Id分组，所以它是map的key
        //value是每组数据 01 音乐  02  小说
        Map<Long, List<BaseCategoryView>> baseCategory1 = baseCategoryViews.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));

        //遍历每一个一级 封装数据  并获取二级三级
        for (Map.Entry<Long, List<BaseCategoryView>> entry : baseCategory1.entrySet()) {

            Long category1Id = entry.getKey();
            List<BaseCategoryView> category1List = entry.getValue();
            JSONObject category1 = new JSONObject();

            category1.put("categoryId", category1Id);
            String category1Name = category1List.get(0).getCategory1Name();
            category1.put("categoryName", category1Name);

            //二级的总集合 存放category2（JSONObject）
            List<JSONObject> category2Child = new ArrayList<>();

            Map<Long, List<BaseCategoryView>> baseCategory2 = category1List.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
            for (Map.Entry<Long, List<BaseCategoryView>> entry2 : baseCategory2.entrySet()) {

                //一个key对应他的1级标题2级标题等
                Long category2Id = entry2.getKey();
                List<BaseCategoryView> category2List = entry2.getValue();

                JSONObject category2 = new JSONObject();
                category2.put("categoryId", category2Id);
                category2.put("categoryName", category2List.get(0).getCategory2Name());

                //stream().map()遍历每个值，并对其操作
                //category2List里的每个值就是上边BaseCategoryView对象
                //get到的category3Id/Value就是  每个category2对应的3的值
                List<JSONObject> category3List = category2List.stream().map(value2 -> {
                    JSONObject category3 = new JSONObject();
                    Long category3Id = value2.getCategory3Id();
                    String category3Name = value2.getCategory3Name();
                    category3.put("categoryId", category3Id);
                    category3.put("categoryName", category3Name);
                    System.out.println(category3Id);
                    return category3;
                }).collect(Collectors.toList());
                System.out.println(category3List);
                category2.put("categoryChild", category3List);
                category2Child.add(category2);
            }
            category1.put("categoryChild", category2Child);
            System.out.println(category1);
            jsonObjects.add(category1);
        }
        return jsonObjects;
    }

    @Override
    public String findCategory3Id(String id) {
        String ids = "100" + id;
        BaseCategory3 baseCategory3 = baseCategory3Mapper.selectById(Long.valueOf(ids));
        return baseCategory3 == null ? "" : baseCategory3.getName();
    }

    @Override
    public List<BaseAttribute> findCategory1Id(String category1Id) {
        List<BaseAttribute> baseAttributeList = baseAttributeMapper.selectList(new LambdaQueryWrapper<BaseAttribute>().eq(BaseAttribute::getCategory1Id, category1Id));
        baseAttributeList.stream().map(temp -> {
            Long id = temp.getId();
            List<BaseAttributeValue> attributeValueList = baseAttributeValueMapper.selectByAttributeId(id);
            temp.setAttributeValueList(attributeValueList);
            return temp;
        }).collect(Collectors.toList());
        return baseAttributeList;
    }

    @Override
    public BaseCategoryView getCategoryBy3(Long category3Id) {
//        baseCategoryViewMapper.selectById()   可以直接用id，因为这个表的3id就是主键
        BaseCategoryView baseCategoryView = baseCategoryViewMapper.selectOne(new LambdaQueryWrapper<BaseCategoryView>().eq(BaseCategoryView::getCategory3Id, category3Id));
        return baseCategoryView;
    }

    @Override
    public List<BaseCategory3> findTopBaseCategory3ByCategory1Id(Long category1Id) {

        List<BaseCategory2> baseCategory2List = baseCategory2Mapper.selectList(new LambdaQueryWrapper<BaseCategory2>().eq(BaseCategory2::getCategory1Id, category1Id));
        List<Long> category2IdList = baseCategory2List.stream().map(b -> b.getId()).collect(Collectors.toList());

//        List<BaseCategory3> baseCategory3List = new ArrayList<>();
//        category2IdList.stream().map(id -> {
//            List<BaseCategory3> baseCategory3Lists = baseCategory3Mapper.selectList(new LambdaQueryWrapper<BaseCategory3>().eq(BaseCategory3::getCategory2Id, id));
//            baseCategory3List.add(baseCategory3Lists);
//        }).collect(Collectors.toList());
//        很显然这样非常麻烦，甚至要嵌套stream流

        LambdaQueryWrapper<BaseCategory3> wrapperBaseCategory3 = new LambdaQueryWrapper<>();
        // (1001,1002,1003)  范围
        wrapperBaseCategory3.in(BaseCategory3::getCategory2Id,category2IdList);
        //is_top=1 置顶
        wrapperBaseCategory3.eq(BaseCategory3::getIsTop,1);
        // limit 7 前7条三级分类数据
        wrapperBaseCategory3.last(" limit 7");
        List<BaseCategory3> category3List = baseCategory3Mapper.selectList(wrapperBaseCategory3);
        return category3List;
    }

    //根据一级分类Id 获取全部数据
    @Override
    public JSONObject getAllCategoryListByCategory1Id(Long category1Id) {
        //1 封装一级分类
        // 根据一级分类id获取一级分类数据，
        BaseCategory1 baseCategory1 = baseCategory1Mapper.selectById(category1Id);
        //封装到JSONObject
        JSONObject category1 = new JSONObject();
        category1.put("categoryId",category1Id);
        category1.put("categoryName",baseCategory1.getName());

        //2 根据一级分类id获取所有二级和三级数据
        LambdaQueryWrapper<BaseCategoryView> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BaseCategoryView::getCategory1Id,category1Id);
        List<BaseCategoryView> categoryViewList = baseCategoryViewMapper.selectList(wrapper);

        //3 向一级分类里面封装所有二级分类
        //根据二级分类id进行分组，map集合
        //map的key是二级分类id，value每组二级分类数据集合
        Map<Long, List<BaseCategoryView>> map =
                categoryViewList.stream()
                        .collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));

        //遍历map集合，封装二级分类
        List<JSONObject> category2Child = new ArrayList<>();
        for(Map.Entry<Long, List<BaseCategoryView>> entry2 : map.entrySet()) {
            //封装二级分类
            Long category2Id = entry2.getKey();
            List<BaseCategoryView> category2List = entry2.getValue();

            JSONObject category2 = new JSONObject();
            category2.put("categoryId",category2Id);
            category2.put("categoryName",category2List.get(0).getCategory2Name());

            //封装三级分类
            // List<BaseCategoryView> -- List<JSONObject>
            List<JSONObject> category3Child = new ArrayList<>();
            category2List.stream().forEach(category3View -> {
                JSONObject category3 = new JSONObject();
                category3.put("categoryId", category3View.getCategory3Id());
                category3.put("categoryName", category3View.getCategory3Name());
                category3Child.add(category3);
            });

            //把三级分类集合放到每个二级分类里面
            category2.put("categoryChild",category3Child);

            category2Child.add(category2);
        }

        //把二级分类放到一级分类里面
        category1.put("categoryChild",category2Child);

        return category1;
    }

    @Override
    public List<BaseCategory1> getCategory1() {
        List<BaseCategory1> baseCategory1List = baseCategory1Mapper.selectList(null);
        return baseCategory1List;
    }
}
